// Data treatment
	
	use "input/20190909 - Base ABC Cerrado.dta" , clear
	
	/* Treatment */
	
		gen CTRL = (tr_Z==0)
		gen CAP = (tr_Z==1)
		gen CAP_ATEG = (tr_Z==2)
		
		label variable CTRL "control"
		label variable CAP "course"
		label variable CAP_ATEG "course + extension"
		
	/* ID, Strata Indicator & State Indicator */
	
		gen id = V00000005
		gen strata = V00000009
		gen uf = V02020100
		gen mun = V02020200
		
		label variable id "identifier"
		label variable strata "group number"
		label variable uf "state"
		label variable mun "municipality"
		
	/* Person who answered the survey */	
	
		rename V01010700 resp
	
		egen temp = sum(resp), by(id)
		
		gen prop = (temp==2)
		
		drop temp
		
	/* Individual characteristics */
	
		gen yob = year(V01010400)
		gen sex = V01010300
		gen res = (V02010700==1)
		gen main = (V02020600==1)
		gen phone = (V02020500==1)
		gen coop = (V02020700==1)
		gen asso = (V02020900)
		
		label variable yob "year of birth"
		label variable sex "gender (0/1)"
		label variable res "lives in the farm (0/1)"
		label variable main "farm is the main source of income (0/1)"
		label variable phone "farm has phone number (0/1)"
		label variable coop "farm belongs to cooperative (0/1)"
		label variable asso "farm belongs to producers' association (0/1)"
	
	/* Land Use */
		
		gen area = V04010000
		
		gen reser = V05010100
		gen sreser = V05010100/V04010000
		
		gen expl = V05010200
		gen sexpl = V05010200/V04010000
		
		gen idle = V05010300
		gen sidle = V05010300/V04010000
		
		gen agric = V05010201 + V05010202
		gen sagric = (V05010201 + V05010202)/V04010000
		
		gen past = V05010203
		gen spast = V05010203/V04010000
		
		gen forest = V05010204
		gen sforest = V05010204/V04010000
		
		gen it = V05010205
		gen sit = V05010205/V04010000
		
		gen pd = V05010202b 
		gen spd = V05010202b/V04010000

		gen prec = V05010203c
		gen sprec = V05010203c/V04010000
		gen erec = (prec>0)
		
		label variable area "total area (ha)"
		
		label variable reser "legal reserves and protected areas (ha)"
		label variable sreser "legal reserves and protected areas (%)"
		
		label variable expl "total land use (ha)"
		label variable sexpl "total land use (%)"
		
		label variable idle "unused land (ha)"
		label variable sidle "unused land (%)"
		
		label variable agric "cropland (ha)"
		label variable sagric "cropland (%)"
		
		label variable past "pastures (ha)"
		label variable spast "pastures (%)"
		
		label variable forest "planted forests (ha)"
		label variable sforest "planted forests (%)"		
		
		label variable it "integrated systems (ha)"
		label variable sit "integrated systems (%)"
		
		label variable pd "no till (ha)"
		label variable spd "no till (%)"
		
		label variable prec "recovered pastures (ha)"
		label variable sprec "recovered pastures (%)"
		label variable erec "recovered pastures (0/1)"
		
	/* Agricultural Practices & Input Use */
	
		gen electr = V07030000
		gen conserv = V07050000a
		gen manage = V07050000b
		gen fires = V07050000c
		gen soil = V07060000
		gen pest = V07100000
		gen rotation = V08010000
		gen machine = V08020000
		gen degrad =  V08040000
		gen sdegrad = V08040100
		gen disease = (V09060000==1)
		gen supl = (V10060000==1) 
		gen supl_salt = (V10060200==1)
		gen supl_feed = (V10060300==1)
		gen insem = (V10080000==1)
		gen mech = (V15010000==1)
		gen nmech = V15000000
		gen pworkers = V16020000
		gen tworkers = V16060000
		gen course = V16040000
		gen extension = V19010000
		replace extension = 0 if V19050000==10 & time==0
		gen supply = (V19020000==3 | V19020000==4 | V19020000==5)
		
		pca conserv manage soil if time==0
		predict mindex		
		
		label variable electr "has electricity (0/1)"
		label variable conserv "# of water and soil conservation practices"
		label variable manage "# of agricultural management practices"
		label variable fires "# of fire related practices"
		label variable soil "did soil analysis in the last 5 years (0/1)"
		label variable pest "uses pesticides (0/1)"
		label variable rotation "rotation of pastures (0/1)"
		label variable machine "uses machines to improve pastures (0/1)"
		label variable degrad "has degraded pastures (0/1)"
		label variable sdegrad "share of degraded pastures (%)"
		label variable disease "disease control (0/1)"
		label variable supl "food suplementation (0/1)"
		label variable supl_salt "food suplementation - salt (0/1)"
		label variable supl_feed "food suplementation - animal feed (0/1)"		
		label variable insem "artificial insemination (0/1)"
		label variable mech "has tractors, machines, vehicles, implements (0/1)"
		label variable nmech "# of tractors, machines, vehicles, implements (0/1)"
		label variable extension "access to extension services (0/1)"
		label variable supply "constraints to access extension services (0/1)"
		label variable mindex "index of agricultural practices"
		
	/* Expenditures, Income & Productivity */
	
		gen inc = V22020000
		gen rev = V22010100/1000
		gen yie = rev/area
		
		winsor2 rev inc yie, cut(1 99)
		
		gen l_rev = log(rev)
		gen l_rev_w = log(rev_w)
		gen l_inc = log(inc)
		gen l_inc_w = log(inc_w)
		gen l_yie = log(yie)
		gen l_yie_w = log(yie_w)
		
		foreach x in V10140102 V10140103 V10140202 V10140203 V10140302 V10140303 V10140402 ///
					 V10140403 V10140502 V10140503 V10140602 V10140603 V10160200 ///
					 V10170102 V10170103 V10170202 V10170203 V10170302 V10170303 ///
					 V10170402 V10170403 V10170502 V10170503 V10170602 V10170603 ///
					 V10180400 V10180500 V10180700 {
			replace `x' = 0 if `x'==.
		}
		
		#delimit ;
		
		gen csales = V10140102*V10140103+V10140202*V10140203+V10140302*V10140303+V10140402*V10140403+
					V10140502*V10140503+V10140602*V10140603+V10160200+V10170102*V10170103+V10170202*V10170203+
					V10170302*V10170303+V10170402*V10170403+V10170502*V10170503+V10170602*V10170603;
		
		gen msales = V10180400*V10180500+V10180700;
		
		#delimit cr
		
		foreach x in V13020109 V13020110 V13020209 V13020210 V13020309 V13020310 V13020409 V13020410 V13020509 V13020510 {
			replace `x' = 0 if `x'==.
		}
		
		gen asales = V13020109*V13020110 + V13020209*V13020210 + V13020309*V13020310 + V13020409*V13020410 + V13020509*V13020510
		
		egen rev2 = rowtotal(csales msales asales)
		
		egen prev = rowtotal(csales msales)
		
		gen pyie = 1+prev / 1+past
		
		winsor2 prev pyie, cut(0 99) 
		
		gen l_prev = log(1+prev)
		gen l_prev_w = log(1+prev_w)
		gen l_pyie = log(1+pyie)
		gen l_pyie_w = log(1+pyie_w)
		
		gen l_csales = log(1+csales)
		gen l_msales = log(1+msales)
		
		encode id, gen(ID)
		
		egen exp = rowtotal( V20020100 V20030100 V20040100 V20050100 V20060100 V20070100 V20080100 V20090100 V20100100 V21010100 V21020100 V21030100 V21040100 V21050100 V21060100 V21070100 V21080100 V21090100 V21100100 V21110100 V21120100 V21130100 V21140100 V21150100 V21160100 V21180100 V21190100)
		
		gen l_exp = log(exp)
		
		egen exp_serv = rowtotal(V20020100 V20030100 V20040100 V20050100 V20060100 V20070100 V20080100 V20090100 V20100100 V21130100 V21150100 V21160100 V21180100 V21190100)

		egen exp_input = rowtotal(V21010100 V21020100 V21030100 V21040100 V21050100 V21060100 V21070100 V21090100 V21100100 V21110100 V21120100)

		gen exp_wage = V21140100
		
		gen exp_fuel = V21080100
		
		foreach x in exp_serv exp_input exp_wage exp_fuel {
			replace `x' = 0 if `x'==.
		}
		
		gen ani = V10010100
		
		gen l_ani = log(ani)
		
		gen l_exp_ani = log(exp/ani)
		
		xtset ID time
		
		keep id time strata mun uf CTRL CAP CAP_ATEG prop yob sex res main phone coop asso area reser sreser expl sexpl idle sidle agric ///
		sagric past spast forest sforest it sit pd spd prec sprec electr conserv manage fires soil pest rotation ///
		machine degrad sdegrad disease supl supl_salt supl_feed insem mech nmech pworkers tworkers course extension ///
		supply inc rev yie rev_w inc_w yie_w l_rev l_rev_w l_inc l_inc_w l_yie l_yie_w csales msales prev pyie prev_w ///
		pyie_w l_prev l_prev_w l_pyie l_pyie_w l_csales l_msales exp l_exp mindex erec wt exp_* ani l_ani l_exp_ani
		
		order id time strata mun uf CTRL CAP CAP_ATEG prop yob sex res main phone coop asso area reser sreser expl sexpl idle sidle agric ///
		sagric past spast forest sforest it sit pd spd prec sprec electr conserv manage fires soil pest rotation ///
		machine degrad sdegrad disease supl supl_salt supl_feed insem mech nmech pworkers tworkers course extension ///
		supply inc rev yie rev_w inc_w yie_w l_rev l_rev_w l_inc l_inc_w l_yie l_yie_w csales msales prev pyie prev_w ///
		pyie_w l_prev l_prev_w l_pyie l_pyie_w l_csales l_msales exp l_exp mindex erec wt exp_* ani l_ani l_exp_ani 
		
	save "output/data.dta" , replace
	


